package mth.rmclusterer;

import java.util.LinkedHashMap;
import java.util.Map;

public class VotedRunResult {
	/*
	 * VotedRunResults stellen für eine FileGroup eine per Mehrheitsentscheid beschlossene
	 * Clusterlösung dar. Die AssingmentsOfRun stellen für jeden Clusterdurchlauf die zugehörigen
	 * Clusterzuweisungen dar. Diese werden pro FileGroup in den Mehrheitsentscheid aufgenommen.
	 */
	private AssignmentsOfRun[] clusterAssignments;
	private String[] assignmentMix;
	private String qValue;
	private String distMeasure;
	private String kValue;
	private String realQValue;
	private String nQValue;
	private String pairCount;
	
	VotedRunResult(String[] files, String qVal, String dMes, String kV,
			String realQVal, String nQVal, String pairC, int rK) {
		
		clusterAssignments = new AssignmentsOfRun[files.length];
		for (int a=0; a<files.length; a++) {
			clusterAssignments[a]=new AssignmentsOfRun(files[a], rK);
		}
		qValue=qVal;
		distMeasure=dMes;
		kValue=kV;
		realQValue = realQVal;
		nQValue = nQVal;
		pairCount = pairC;
		assignmentMix = imputeClusterAssignments();
	}
	
	public String[] getAssignmentMix() {
		return assignmentMix;
	}
	
	public String getDistanceMeasure() {
		return distMeasure;
	}
	
	public String getQValue() {
		return qValue;
	}
	
	public String getRealQValue() {
		return realQValue;
	}
	
	public String getNQValue() {
		return nQValue;
	}
	
	public String getPairCount() {
		return pairCount;
	}
	
	public String getKValue() {
		return kValue;
	}
	
	private String[] imputeClusterAssignments() {
		String[][] tmpValues = new String[clusterAssignments[0].getClusterRunValues().length][clusterAssignments.length];
		for (int a=0; a<clusterAssignments.length; a++) {
			for (int b=0; b<clusterAssignments[0].getClusterRunValues().length; b++) {
				tmpValues[b][a]=clusterAssignments[a].getClusterRunValues()[b];
			}
		}
		
		String[] retValues = new String[tmpValues.length];
		for (int a=0; a<tmpValues.length; a++) {
			retValues[a]=voteElements(tmpValues[a]);
		}
		return retValues;
	}
	
	private String voteElements(String[] val) {
		Map <String, Integer> m = new LinkedHashMap<String, Integer>();
		String[] distinctVal = new String[0];
		for (int a=0; a<val.length; a++) {
			if (m.size()==0) {
				m.put(val[a], 1);
				distinctVal=Manager.addStringToArray(distinctVal, val[a]);
			} else {
				if (Manager.stringArrayContains(distinctVal, val[a])) {
					m.put(val[a], m.get(val[a])+1);
				} else {
					m.put(val[a], 1);
					distinctVal=Manager.addStringToArray(distinctVal, val[a]);
				}
			}
		}
		String retString="";
		int maxCount=0;
		for (int a=0; a<distinctVal.length; a++) {
			if (m.get(distinctVal[a])>maxCount) {
				retString=distinctVal[a];
				maxCount=m.get(distinctVal[a]);
			}
		}
		return retString;
	}
	
}
